function [matOut,rightVec,cellOut]=struct2matMatch(strucPivot,strucTwo) 
% =====================================================================
%
% [matOut,rightVec,cellOut]=struct2matMatch(strucPivot,strucTwo) 
%
% Given the structure strucPivot with N1 fields, use its fieldnames to find the
% corresponding entries in strucTwo with N2 fields and create a matrix that maps the right
% rows. All N1 entries of strucPivot must be a subset of strucTwo, where N1
% <=N2. 
% 
% matOut    [N1 2]  Matrix 
% rightVec  [N1 1]  Second column of matrix, corresponding to the elements
%                   of strucTwo in the same order as strucPivot 
% cellOut   [N1 3]  Cell with the names and values (to 5 decimal places) 
% 
% Related: struc2mat strucMult2mat which calls this function multiple times 
%
% Alejandro Justiniano Feb 2013 
% =====================================================================
fields=fieldnames(strucPivot); 
Nfields=length(fields); 
matOut =zeros(Nfields,2); 
matOut(:,1)=cell2mat(struct2cell(strucPivot)); 
fieldsTwo=fieldnames( strucTwo );
for jj=1:Nfields;
    pos=strcmp(char(fields(jj)),fieldsTwo);
    if all(pos==0);
        error([char(fields(jj)),' could not be found']);
    end
    pos=find(pos~=0);
    if length(pos)~=1
        error('Only one position can match');
    end
    matOut(jj,2)=strucTwo.(fieldsTwo{pos});
end
rightVec=matOut(:,2); 
cellOut=[fields(:) num2cprec(matOut,5)]; 